<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Models\Es\EsMyIndexModel;
use Illuminate\Http\Request;


class TestController extends ApiController
{

    /**
     * @OA\Get(
     *     path="/test",
     *     tags={"对外服务"},
     *     summary="测试用例",
     *     description="一个测试用的控制器",
     *     operationId="testController",
     *     @OA\Response(
     *         response=500,
     *         description="请求错误示例",
     *         @OA\JsonContent(ref="#/components/schemas/TestApi"),
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="请求成功示例",
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property(property="id", type="integer", description="主键ID"),
     *                 @OA\Property(property="name", type="string", description="名称"),
     *             ),
     *         )
     *     ),
     * )
     * @OA\Schema(
     *   schema="TestApi",
     *   allOf={
     *     @OA\Schema(
     *       @OA\Property(property="user_id", type="integer", description="用户ID"),
     *       @OA\Property(property="email", type="string", description="email"),
     *     )
     *   }
     * )
     */
    public function index()
    {
        $data = ['user' => 'abc', 'title' => 'bbc', 'desc' => '123', 'pass' => '123456', 'age' => 18, 'birthday' => '2019-11-12 00:00:00'];
        $a = EsMyIndexModel::query()->insert($data);
        dd($a);
        $es = es();
        $assetsEs = ['index' => 'assets', 'type' => '_doc'];
        $where = [];
//        $where[] = ["term" => ['age' => 18]];
        $list = $this->esSelectCount($es, $assetsEs, $where, 10);
        dd($list);
//        $listTow = EsMyIndexModel::query()->get()->toArray();
        $map = [];
        foreach ($list["hits"]["hits"] as $val) {
            $_source = $val["_source"];
            $map[] = [
                "user" => $_source["user"],
                "title" => $_source["title"],
                "desc" => $_source["desc"],
                "pass" => $_source["pass"],
                "age" => $_source["age"],
                "birthday" => $_source["birthday"],
            ];
        }
        return $this->apiResponse($map);
    }

    public function esSelectCount($es, $assetsEs, $where, $size = 50000)
    {
        return $es->search([
            "index" => $assetsEs['index'],
            "type" => $assetsEs['type'],
            "body" => [
                "size" => $size,
                "query" => [
                    "bool" => [
                        "filter" => $where,
                    ],
                ],
            ],
        ]);
    }

}

